package com.wu.basic.dynamic.controller;

import com.wu.basic.dynamic.domain.uo.EnglishWord;
import com.wu.basic.dynamic.util.TTSUtil;
import com.wu.framework.inner.layer.web.EasyController;
import com.wu.framework.inner.lazy.database.expand.database.persistence.LazyOperation;
import org.springframework.boot.CommandLineRunner;
import org.springframework.util.ObjectUtils;

import java.util.List;

@EasyController("/english")
public class EnglishWordDictionaryController implements CommandLineRunner {

    private final LazyOperation operation;

    public EnglishWordDictionaryController(LazyOperation operation) {
        this.operation = operation;
    }

    @Override
    public void run(String... args) throws Exception {
        // 测试创建表
        final EnglishWord simple = new EnglishWord();
        simple.setWord("测试数据");
        operation.smartUpsert(simple);

        System.out.println("自动翻译单词语音开始");
        final List<EnglishWord> englishWords = operation.executeSQL("select id,word,translation from english_word where voice is  null limit 100 ", EnglishWord.class);
        if (!ObjectUtils.isEmpty(englishWords)) {
            for (EnglishWord englishWord : englishWords) {
                englishWord.setVoice(TTSUtil.baiduTextToSpeech(englishWord.getWord()));
            }
            operation.upsert(englishWords);
            run(args);
        }
        System.out.println("自动翻译单词语音结束");
    }


}
